﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.StorageClient;
using Microsoft.WindowsAzure.ServiceRuntime;
using System.IO;

namespace WCFServiceWebRole1
{
    public class StorageRetainer
    {
        public static string exMsg;
        /// <summary>
        /// Store an Image on Blob. Returns true if successful.
        /// </summary>
        /// <param name="fileName">Name of Image. Example asd.jpg</param>
        /// /// <param name="imgType">Type of Image. 1:User, 2:Reward, 3: Badge</param>
        /// /// <param name="userImg">byte[] of the Image</param>
        /// <returns>bool</returns>
        public static bool ImgStore(string fileName, byte[] userImg)
        {
            fileName += ".png";
            //if (imgType != 1 || imgType != 2 || imgType != 3)
            //{
            //    return null;
            //}
            string containerRef = "comimg";
            bool success = false;
            try
            {
                CloudStorageAccount.SetConfigurationSettingPublisher((configName, configSetter) =>
                {
                    configSetter(RoleEnvironment.GetConfigurationSettingValue(configName));
                });
                // Get the storage account reference
                CloudStorageAccount blobStore = CloudStorageAccount.FromConfigurationSetting("ToTBlob");
                // get the Client reference using storage blobend point
                CloudBlobClient objClient = new CloudBlobClient(blobStore.BlobEndpoint, blobStore.Credentials);
                // Get Container reference
                CloudBlobContainer objContainer = objClient.GetContainerReference(containerRef);
                // Get blob reference
                CloudBlob obj = objContainer.GetBlobReference(fileName);

                // Set meta values
                obj.Metadata["MetaName"] = "meta";
                // Open a stream using the cloud object
                BlobStream blobstream = obj.OpenWrite();
                // Write the stream to the blob database
                blobstream.Write(userImg, 0, userImg.Count());
                blobstream.Close();
                success = true;
                //returnUri = fileName;
            }
            catch (Exception e)
            {
                exMsg = e.ToString();
                return false;
            }
            return success;
        }

        /// <summary>
        /// Retrieve an Image from Blob
        /// </summary>
        /// <param name="fileName">Name of Image. Example asd.jpg</param>
        /// /// <param name="imgType">Type of Image. 1:User, 2:Reward, 3: Badge</param>
        /// <returns>byte[]</returns>
        public static byte[] ImgRetrieval(string fileName)
        {
            fileName += ".png";
            string containerRef = "comimg";
            byte[] img = null;
            try
            {
                CloudStorageAccount.SetConfigurationSettingPublisher((configName, configSetter) =>
                {
                    configSetter(RoleEnvironment.GetConfigurationSettingValue(configName));
                });
                // Get the storage account reference
                CloudStorageAccount blobStore = CloudStorageAccount.FromConfigurationSetting("ToTBlob");
                // get the Client reference using storage blobend point
                CloudBlobClient objClient = new CloudBlobClient(blobStore.BlobEndpoint, blobStore.Credentials);
                // Get Container reference
                CloudBlobContainer objContainer = objClient.GetContainerReference(containerRef);
                // Get blob reference
                CloudBlob obj = objContainer.GetBlobReference(fileName);
                BlobStream blobstream = obj.OpenRead();
                // Create the image object and display the same on the browser response
                System.Drawing.Image objimg = null;
                objimg = System.Drawing.Image.FromStream(blobstream, true);

                //convert from Drawing.Image to byte[]
                MemoryStream ms = new MemoryStream();
                objimg.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                img = ms.ToArray();
            }
            catch (Exception e)
            {
                exMsg = e.ToString();
            }
            return img;
        }
    }
}